package cn.myjava.gupao.mode.prototype;

import java.io.*;
import java.util.Date;

/**
 * Created by RenFu
 * Date: 2019/5/17
 */
public class StudentHistoryBook extends Student2 implements Cloneable, Serializable {

    public HistoryBook historyBook;

    public StudentHistoryBook() {
        this.birthday = new Date();
        this.historyBook = new HistoryBook();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return this.doClone();
    }

    /**
     * 深克隆
     */
    public Object doClone() {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(this);

            bais = new ByteArrayInputStream(baos.toByteArray());
            ois = new ObjectInputStream(bais);
            StudentHistoryBook studentHistoryBook = (StudentHistoryBook) ois.readObject();
            return studentHistoryBook;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (oos != null) {
                    oos.close();
                }
                if (bais != null) {
                    bais.close();
                }
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
